import {
  wechatConfigList,
  wecharConfigUpdate,
  wechatUserList,
  wechatconfigadd,
  wechatDeleteConfig,
} from '@/services/wechat';
import { message } from 'antd';

export default {
  namespace: 'wechat',
  state: {
    list: [],
    pagination: [],
  },
  effects: {
    *fetchwechatConfigList({ payload }, { call, put }) {
      const response = yield call(wechatConfigList, payload);
      if (response && response.status === 'success') {
        yield put({
          type: 'wechatConfigList',
          payload: response.data,
        });
      }
    },
    *fetchwecharConfigUpdate({ payload }, { call, put }) {
      const response = yield call(wecharConfigUpdate, payload);
      if (response && response.status === 'success') {
        yield put({
          type: 'wecharConfigUpdate',
          ...payload,
        });
        message.success('修改成功');
      }
    },
    *DeleteConfig({ payload }, { call, put }) {
      const response = yield call(wechatDeleteConfig, payload);
      if (response && response.status === 'success') {
        yield put({
          type: 'wechatConfigDelete',
          ...payload,
        });
        message.success('删除成功');
      }
    },
    *ConfigAdd({ payload }, { call, put }) {
      const response = yield call(wechatconfigadd, payload);
      if (response && response.status === 'success') {
        const tworesponse = yield call(wechatConfigList);
        if (tworesponse && tworesponse.status === 'success') {
          yield put({
            type: 'wechatConfigList',
            payload: tworesponse.data,
          });
        }
        message.success('添加成功');
      }
    },

    *fetchwechatUserList({ payload }, { call, put }) {
      const response = yield call(wechatUserList, payload);
      if (response && response.status === 'success') {
        yield put({
          type: 'wechatUserList',
          payload: response.data,
        });
      }
    },
  },
  reducers: {
    wechatConfigList(state, action) {
      return {
        ...state,
        list: action.payload,
      };
    },
    wecharConfigUpdate(state, action) {
      for (let i = 0; i < state.list.length; i++) {
        if (state.list[i].id === action.id) {
          state.list[i] = action;
        }
      }
      return {
        ...state,
      };
    },
    wechatConfigDelete(state, action) {
      console.log(action);
      console.log(Array.isArray(action.key));
      if (Array.isArray(action.key)) {
        for (let i = 0; i < state.list.length; i++) {
          for (let l = 0; l < action.key.length; l++) {
            if (state.list[i].key === action.key[l]) {
              state.list.splice(i, 1);
            }
          }
        }
      } else {
        for (let i = 0; i < state.list.length; i++) {
          if (state.list[i].key === action.key) {
            state.list.splice(i, 1);
          }
        }
      }

      return {
        ...state,
      };
    },
    wechatUserList(state, action) {
      return {
        ...state,
        list: action.payload.list,
        pagination: action.payload.pagination,
      };
    },
  },
};
